package com.example.demo_flow

import android.net.Uri
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 */
class FlowActivity : AppCompatActivity() {
//    private val sharedFlow = MutableSharedFlow<Int>(1,2,BufferOverflow.DROP_OLDEST)
    private val sharedFlow = MutableStateFlow<Int>(0) //1,0,BufferOverflow.DROP_OLDEST
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_flow)
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                //先发射
//                sharedFlow.emit(0)
//                sharedFlow.emit(1)
//                sharedFlow.emit(2)
                //后收集，后注册
                sharedFlow.collect{
                    delay(1000)
                    println("collect $it")
                }
            }
        }
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                sharedFlow.emit(3)
                sharedFlow.emit(4)
                sharedFlow.emit(5)
                sharedFlow.emit(6)
            }
        }

    }
}
